package util;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.FileOutConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;
import java.util.List;

/**
 * 代码自动生成
 * @author ErKang
 * @date Created in 2021/8/27
 * @title MybatisPlusAutoGenerator.java
 * Department: Product development
 */
public class MybatisPlusAutoGenerator {

    String dbUrl = "jdbc:mysql://localhost:3306/lancaster?zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&useUnicode=true" +
            "&allowMultiQueries=true";

    String driverClassName = "com.mysql.jdbc.Driver";

    String username = "root";

    String password = "WYziVnlqDUn1OwylhyCD";

    private String[] dbTableList = new String[]{"lan_activity_code"};

    public static void main(String[] args) {
        MybatisPlusAutoGenerator generator = new MybatisPlusAutoGenerator();
        generator.autoGenerator();
    }

    public void autoGenerator() {
        AutoGenerator autoGenerator = new AutoGenerator();
        // ========= 全局配置
        GlobalConfig gc = new GlobalConfig();
        // 用户目录
        String projectPath = System.getProperty("user.dir") + "/lancaster-model/gen";
        // 输出目录
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("xyqierkang@163.com");
        // 生成文件后是否打开资源管理器
        gc.setOpen(false);
        // 不要重新生成覆盖
        gc.setFileOverride(true);
        gc.setEntityName("%sDO");
        // 去掉接口前面的字幕I
        //        gc.setServiceName("%sService");

        // =========== 主键策略
        gc.setIdType(IdType.AUTO);
        gc.setDateType(DateType.ONLY_DATE);
        autoGenerator.setGlobalConfig(gc);

        // ============ 数据库配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl(dbUrl);
        dataSourceConfig.setDriverName(driverClassName);
        dataSourceConfig.setUsername(username);
        dataSourceConfig.setPassword(password);
        dataSourceConfig.setDbType(DbType.MYSQL);
        autoGenerator.setDataSource(dataSourceConfig);

        // ============= 自动生成包配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("com.cloud.lan");
        packageConfig.setService("service");
        packageConfig.setMapper("dao");
        packageConfig.setEntity("dataobject");
        autoGenerator.setPackageInfo(packageConfig);

        // ========== 自定义Xml输出配置
        // 如果模板引擎是 freemarker
//         String templatePath = "/templates/mapper.xml.ftl";
        // 如果模板引擎是 velocity
        String templatePath = "/templates/mapper.xml.vm";
        // 自定义输出配置
        List<FileOutConfig> focList = new ArrayList<>();
        // 自定义配置会被优先输出
        focList.add(
                new FileOutConfig(templatePath) {
                    @Override
                    public String outputFile(TableInfo tableInfo) {
                        // 自定义输出文件名
                        return projectPath
                                + "/src/main/resources/mapper"
                                + "/"
                                + tableInfo.getEntityName()
                                + "Mapper"
                                + StringPool.DOT_XML;
                    }
                });
        InjectionConfig cfg =
                new InjectionConfig() {
                    @Override
                    public void initMap() {
                        // to do nothing
                    }
                };
        cfg.setFileOutConfigList(focList);
        autoGenerator.setCfg(cfg);

        // ========== 模板配置
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setController(null);
        templateConfig.setXml(null);
        autoGenerator.setTemplate(templateConfig);

        // ============ 策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setInclude(dbTableList);
        // 表名下划线转驼峰
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        // 自动生成驼峰
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        // 生成lombok
        strategyConfig.setEntityLombokModel(true);

        // =========== 自动填充配置
        TableFill createTime = new TableFill("ctime", FieldFill.INSERT);
        TableFill updateTime = new TableFill("mtime", FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> list = new ArrayList<>();
        list.add(createTime);
        list.add(updateTime);
        strategyConfig.setTableFillList(list);
        autoGenerator.setStrategy(strategyConfig);
        autoGenerator.execute();
    }
}
